<template>
	<view class="p-10">
		<view class="card">
			<my-forms ref="valiForm" :model="valiFormData" :label-position="'top'" :formItem="formData"
				label-width='120px'></my-forms>
			<u-button type="primary" shape="circle" text="确认提交" @click="submit"></u-button>
		</view>
	</view>
</template>
<script setup>
	import {
		onLoad,
	} from '@dcloudio/uni-app'
	import {
		computed,
		reactive,
		ref
	} from 'vue'
	import {
		getapplyInfoadd
	} from '@/api/flowPath.js'
	import {
		listRiskPointManagement
	} from '@/api/riskcontrol.js'
	import {
		getDicts
	} from '@/api/public.js'


	// 校验表单数据
	let formData = computed(() => {
		return [{
				itemType: 'select',
				type: 'GETuseList',
				label: '申请负责人',
				required: true,
				prop: 'applyDeptLeaderUserId'
			},
			{
				itemType: 'cascader',
				type: 'GETdeptTree',
				label: '申请部门',
				required: true,
				prop: 'applyDeptId'
			},
			{
				itemType: 'date',
				type: 'datetime',
				label: '申请时间',
				required: true,
				prop: 'applyDate'
			},
			{
				itemType: 'date',
				type: 'datetime',
				label: '开始时间',
				required: true,
				prop: 'startTime'
			},
			{
				itemType: 'date',
				type: 'datetime',
				label: '结束时间',
				required: true,
				prop: 'endTime'
			},
			{
				label: '作业风险地点',
				prop: 'locationId',
				required: true,
				itemType: 'select',
				list: checkList.value,
			},
			{
				label: '作业类型',
				prop: 'highRiskWorkType',
				required: true,
				itemType: 'select',
				list: highRisk.value,
			},
			{
				label: '作业标题',
				prop: 'highRiskWorkTitle',
				required: true,
			},
			{
				label: '作业部门',
				prop: 'workDeptId',
				required: true,
				itemType: 'cascader',
				type: 'GETdeptTree',
			},
			{
				label: '作业负责人',
				prop: 'siteLeaderUserId',
				required: true,
				itemType: 'select',
				type: 'GETuseList',
			},
			{
				label: '参与人员',
				prop: 'joinUsers',
				required: true,
				type: 'GETuseList',
				itemType: 'select',
				multiple: true
			},
			{
				label: '作业内容描述',
				prop: 'workInfo',
				required: true,
			},
			{
				label: '作业步骤描述',
				prop: 'workProcessInfo',
				required: true,
			},
			{
				label: '作业安全措施描述',
				prop: 'workControlInfo',
				required: true,
			}
		].filter(i => i)
	})
	const valiForm = ref(null)
	const valiFormData = reactive({})
	// 保存提交
	const submit = () => {
		valiForm.value.$refs.refMyForm.validate().then(async (res) => {
			console.log('success', valiFormData);
			const {
				code,
				msg
			} = await getapplyInfoadd(valiFormData)
			if (code == 200) {
				uni.showToast({
					icon: 'success',
					title: `新增成功`
				})
				uni.navigateBack({
					delta: 1 // 返回的页面数，如果是1则返回上一页
				})
			} else {
				uni.showToast({
					title: msg
				})
			}
		}).catch(err => {
			console.log('err', err);
		})
	}

	onLoad((e) => {
		valiFormData.procdefId = JSON.parse(e.procdefId)
	})

	let checkList = ref([]),
		highRisk = ref([])
	const getCheckPonit = async () => {
		const {
			data
		} = await listRiskPointManagement()
		checkList.value = data.map((item) => ({
			dictLabel: item.riskLocationName,
			dictValue: item.id
		}))
	}
	const gethighRisk = async () => {
		const {
			data
		} = await getDicts('high_risk_work_type')
		highRisk.value = data.map((item) => ({
			dictLabel: item.dictLabel,
			dictValue: item.dictValue
		}))
	}
	getCheckPonit()
	gethighRisk()
</script>
<style lang="scss" scoped>
</style>